$(function(){
	
	var page=1;
	var tddata=[
		{
			key:'number',
			type:1,
			url:''
		},{
			key:'car_type_name',
			type:1,
			url:'{:url("detail")}',
			id:137
		},{
			key:'name',
			type:1,
			url:''
		},{
			key:'engine_no',
			type:1,
			url:''
		},{
			key:'displacement',
			type:1,
			url:''
		},{
			key:'source2',
			type:1,
			url:''
		},{
			key:'energy_type2',
			type:1,
			url:''
		},{
			key:'buy_money',
			type:1,
			url:''
		},
		{
			key:'buy_date',
			type:1,
			url:''
		},
		{
			key:'colour',
			type:1,
			url:''
		},
		{
			key:'car_age',
			type:1,
			url:''
		},{
			key:'mileage',
			type:1,
			url:''
		},
		{
			key:'gearbox_type2',
			type:1,
			url:''
		},{
			key:'carry',
			type:1,
			url:''
		},
		{
			key:'structure2',
			type:1,
			url:''
		},
		{
			key:'prop',
			type:1,
			url:''
		},
		{
			key:'driving_license_issued',
			type:1,
			url:''
		},
		{
			key:'buy_money',
			type:1,
			url:''
		},
		{
			key:'company_name',
			type:1,
			url:''
		},
		{
			key:'entrust_use_id',
			type:1,
			url:''
		},
		{
			key:'entrust_rental_id',
			type:1,
			url:''
		},
		{
			key:'rent_company',
			type:1,
			url:''
		}];
	
	var addoperation=``;
	var fields='';
	
	var data={
		page:1,
		pagenum:10
	}
	
	var where_json={}
	{if $Think.get.number}
		where_json.number=['like','%{$Think.get.number}%']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	{if $Think.get.car_type_name}
		where_json.car_type_name=['like','%{$Think.get.car_type_name}%']
	{/if}
	{if $Think.get.is_check}
		where_json.is_check=['in','{$Think.get.is_check}']
	{/if}
	
	data.where_json=JSON.stringify(where_json)
	ajaxcar(data)
	function ajaxcar(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:'{:url("adminapi/Car/carList")}',
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
				for(var i=0;i<res.data.length;i++){
					if(res.data[i].source==1){
						res.data[i].source2='单位直属公务车'
					}else if(res.data[i].source==2){
						res.data[i].source2='管理公司直属公务车'
					}else if(res.data[i].source==3){
						res.data[i].source2='单位自有车辆'
					}else{
						res.data[i].source2='来源未知'
					}
					if(res.data[i].energy_type==1){
						res.data[i].energy_type2='电动'
					}else if(res.data[i].energy_type==2){
						res.data[i].energy_type2='汽油'
					}else if(res.data[i].energy_type==3){
						res.data[i].energy_type2='柴油'
					}else{
						res.data[i].energy_type2='类型未知'
					}
					if(res.data[i].gearbox_type==1){
						res.data[i].gearbox_type2='手动'
					}else if(res.data[i].gearbox_type==2){
						res.data[i].gearbox_type2='自动'
					}else if(res.data[i].gearbox_type==3){
						res.data[i].gearbox_type2='手自一体'
					}else{
						res.data[i].gearbox_type2='类型未知'
					}
					if(res.data[i].structure==1){
						res.data[i].structure2='两厢车'
					}else if(res.data[i].structure==2){
						res.data[i].structure2='三厢车'
					}else if(res.data[i].structure==3){
						res.data[i].structure2='SUV'
					}else{
						res.data[i].structure2='车身结构未知'
					}
				}
//				$('tbody').html(tbodymainf2(res.data, 1, tddata, addoperation, 'driver_id'))
					
				layui.use('table', function(){
				  	var table = layui.table;
				 //展示已知数据
				 	 table.render({
				    	elem: '#car'
					    ,cols: [[ //标题栏
					     {field: 'number', title: '车牌号', width: 150,fixed: 'left'}
					      ,{field: 'car_type_name', title: '车型', minWidth: 150}
					      ,{field: 'name', title: '品牌', minWidth: 150}
					      ,{field: 'engine_no', title: '发动机号', width: 180}
					      ,{field: 'displacement', title: '排量', minWidth: 150}
					      ,{field: 'source2', title: '来源', minWidth: 150}
					      ,{field: 'energy_type2', title: '能源类型', width: 150}
					      ,{field: 'buy_money', title: '保险费用', minWidth: 150}
					      ,{field: 'buy_date', title: '购买日期', minWidth: 150}
					      ,{field: 'colour', title: '车辆颜色', width: 150}
					      ,{field: 'car_age', title: '车龄', minWidth: 150}
					      ,{field: 'mileage', title: '行驶里程', minWidth: 150}
					      ,{field: 'gearbox_type2', title: '变速箱类型', width: 150}
					      ,{field: 'carry', title: '承载人数', minWidth: 150}
					      ,{field: 'structure2', title: '车身结构', minWidth: 150}
					      ,{field: 'prop', title: '车辆性质', minWidth: 150}
					      ,{field: 'driving_license_issued', title: '行驶证发放日期', width: 150}
					      ,{field: 'buy_money', title: '购买金额', minWidth: 150}
					      ,{field: 'company_name', title: '所属公司', minWidth: 150}
					      ,{field: 'entrust_use_id', title: '委托公司（租车）', width: 150}
					      ,{field: 'entrust_rental_id', title: '委托公司（用车）', minWidth: 150}
					      ,{field: 'rent_company', title: '租赁公司', minWidth: 150}
					      
					    ]]
					    ,data:res.data
					    //,skin: 'line' //表格风格
					    ,even: true
					    //,page: true //是否显示分页
					    //,limits: [5, 7, 10]
					    //,limit: 5 //每页默认显示的数量
				  	});
					$('.pagination').html(pagechang1(res.count, 10, page))
					$('.laquo').on('click',function(){
						console.log(data)
						page-=1;
						data.page=page
						ajaxcar(data)
					})
					$('.pageli').on('click',function(){
						console.log(data)
						page=$(this).html()
						data.page=page
						ajaxcar(data)
					})
					//下一页
					$('.raquo').on('click',function(){
						console.log(data)
						page+=1;
						data.page=page
						ajaxcar(data)
					});
				})
				$('.daochu').on('click',function(){
						var getkey="";
	//					for(var key in res.description){
	//						getkey+=`<li>
	//									<label class="am-checkbox">
	//								      <input type="checkbox" value="" data-key='${key}' checked> ${res.description[key].name}
	//								    </label>
	//								</li>`
	//					}
						
	
						for(var iii=0;iii<tddata.length;iii++){
							if(tddata[iii].key=='source2'){
								for(var key in res.description){
									if(key=='source'){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='source' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}else if(tddata[iii].key=='energy_type2'){
								for(var key in res.description){
									if(key=='energy_type'){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='energy_type' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}else if(tddata[iii].key=='gearbox_type2'){
								for(var key in res.description){
									if(key=='gearbox_type'){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='gearbox_type' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}else if(tddata[iii].key=='structure2'){
								for(var key in res.description){
									if(key=='structure'){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='structure' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}else{
								for(var key in res.description){
									if(key==tddata[iii].key){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='${tddata[iii].key}' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}
							
						}
						layer.open({
						  	type: 1,
						  	title:false,
						  	skin: 'layui-layer-rim', //加上边框
						  	area: ['300px', '400px'], //宽高
						  	content: `<div>
						 				<div style='height:300px;overflow:auto;margin-bottom:30px'>
									  	<ul class='daochuul'>${getkey}</ul>
									  	</div>
									  	<div style='text-align:center'>
									  		<button class='am-btn am-btn-sm am-btn-primary am-radius daochu_ok' type='button'>确定导出</button>
									  		<button class='am-btn am-btn-sm am-btn-primary am-radius' type='button'>取消</button>
									  	</div>
									</div>`,
							success:function(){
								var ddata={}
								$('.daochu_ok').on('click',function(){
									ddata.fields="";
									var where_jsondao={};
									{if $Think.get.name}
										where_jsondao.name=['like','%{$Think.get.name}%']
									{/if}
									{if $Think.get.phone}
										where_jsondao.phone=['like','%{$Think.get.phone}%']
									{/if}
									{if $Think.get.company_name}
										where_jsondao.company_name=['like','%{$Think.get.company_name}%']
									{/if}
									{if $Think.get.number}
										where_jsondao.number=['like','%{$Think.get.number}%']
									{/if}
									
									ddata.where_json=JSON.stringify(where_jsondao)
									
									$('.daochuul li input[type="checkbox"]').each(function(){
										if(this.checked){
											ddata.fields+=($(this).data('key')+",")
										}
									})
									ddata.fields=ddata.fields.slice(0,ddata.fields.length-1)
									console.log(ddata.fields)
									console.log(`{:url("adminapi/Driver/downLoadDriverList")}??where_json=${ddata.where_json}&fields=${ddata.fields}`)
									window.open(`{:url("adminapi/Car/downLoadCarList")}??where_json=${ddata.where_json}&fields=${ddata.fields}`)
	//								
								})
								
							}
						});
					})
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
})
